import { IList } from './../interface';
import { extend, RequestMethod, ResponseError } from 'umi-request';

interface IApiActionOption {
  pathName: string;
  request: RequestMethod;
}

interface IApiOption {
  pathName: string;
  errorHandler: (error: ResponseError) => void;
}

export function useListApi<T>(op: IApiActionOption) {
  return (pn: number) => op.request<IList<T>>(`${op.pathName}/list`);
}

export function useInfoApi<T>(op: IApiActionOption) {
  return (id: number | string) => op.request<T>(`${op.pathName}/${id}`);
}

export function createBaseApi({ pathName, errorHandler }: IApiOption) {
  const request = extend({
    errorHandler, // 默认错误处理
    headers: {
      miaover: 'v2',
    },
  });

  const actionOp: IApiActionOption = {
    request,
    pathName,
  };

  return actionOp;
}
